<template>
  <div>
    <el-table :data="list" border style="width: 100%">
      <el-table-column prop="username" label="姓名" width="180" />
      <el-table-column prop="sex" label="性别" width="180" />
      <el-table-column prop="age" label="年龄" width="180" />
      <el-table-column prop="graduation" label="是否毕业" width="250" />
      <el-table-column prop="intro" label="个人介绍" width="400" />
      <el-table-column>
        <template #header>
          <el-input v-model="search" placeholder="输入关键字搜索" />
        </template>
        <template v-slot="scop">
          <el-button>毕业</el-button>
          <el-button @click="del(scop.row._id)" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pagenum"
      v-model:page-size="pagesize"
      :page-sizes="[3, 5, 8, 15]"
      :small="false"
      :disabled="false"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="currentChange"
    />
  </div>
</template>

<script setup>
import { ref, toRefs, reactive } from "vue";
import { getStudents, delStu } from "@/api/api";
// import { useRoute } from "vue-router";

// 父传子
let prop = defineProps({
  str: {
    type: String,
  },
});

console.log(prop.str);

// 搜索框
let search = ref("");
// 路由信息对象
// let route = useRoute();
// 数据
let data = reactive({
  major: prop.str,
  pagenum: 1,
  pagesize: 3,
  total: 0,
});
// 数据解构并响应式
let { major, pagenum, pagesize, total } = toRefs(data);
import { ElMessage } from "element-plus";

// 表格渲染
let list = ref([]);

// 获取列表

let getList = () => {
  getStudents(data).then((res) => {
    console.log(res);
    list.value = res.data;
    data.total = res.total;
  });
};

// 调用接口
getList();

// 每页显示页数
let sizeChange = (val) => {
  data.pagesize = val;
  console.log(11);
  getList();
};
// 当前页数
let currentChange = (val) => {
  data.pagenum = val;
  getList();
};

// 删除
let del = (id) => {
  delStu(id).then((res) => {
    console.log(res);
    if (res.code == 200) {
      ElMessage({
        message: res.msg,
        type: "success",
      });
    } else {
      ElMessage.error(res.msg);
    }
    getList();
  });
};
</script>

<style lang="scss" scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}

.el-pagination {
  background: #fff;
  height: 60px;
}
</style>